local skill = fk.CreateSkill {
  name = "lvshi__snatch_skill",
}

local ls = require "packages/lvshi/utility"
local DIY = require "packages/diy_utility/diy_utility"
local snatch_skill = Fk.skills["snatch_skill"] --[[ @as ActiveSkill ]]

skill:addEffect("cardskill", {
  prompt = "#lvshi__snatch_skill",
  target_num = 1,
  distance_limit = 1,
  mod_target_filter = snatch_skill.modTargetFilter,
  target_filter = Util.CardTargetFilter,
  on_use = function(self, room, use)
    local choice = ls.strategytrickuse(use.from, use.card, "snatch_scheme", "snatch_courage")
    use.extra_data = use.extra_data or {}
    use.extra_data.strategy = choice
  end,
  on_effect = function(self, room, effect)
    if effect.from.dead or effect.to.dead or effect.to:isAllNude() then return end
    if effect.extra_data.strategy == "snatch_scheme" then
      local cards = {}
      if effect.to:getHandcardNum() > 0 then table.insertIfNeed(cards, {"$Hand", effect.to:getCardIds("h")}) end
      if #effect.to:getCardIds("e") > 0 then table.insertIfNeed(cards, {"$Equip", effect.to:getCardIds("e")}) end
      if #effect.to:getCardIds("j") > 0 then table.insertIfNeed(cards, {"$Judge", effect.to:getCardIds("j")}) end
      local id = room:askToChooseCard(effect.from, { target = effect.to, flag = { card_data = cards }, skill_name = "snatch" })
      room:obtainCard(effect.from, id, false, fk.ReasonPrey, effect.from, "snatch_skill")
      DIY.showCards(effect.from, id)
      local card = Fk:getCardById(id)
      if (card.trueName == "jink" or card.trueName == "peach") and effect.to:isAlive() then
        effect.to:drawCards(2, self.name)
      end
    else
      if effect.extra_data.strategy == "snatch_courage" then
        local slashs = table.filter(effect.to:getCardIds("h"), function (id) return Fk:getCardById(id).trueName == "slash" end)
        if #slashs > 0 then
          DIY.showCards(effect.to, slashs)
        end
      end
      local cid = room:askToChooseCard(effect.from, { target = effect.to, flag = "hej", skill_name = "snatch" })
      room:obtainCard(effect.from, cid, false, fk.ReasonPrey, effect.from, "snatch_skill")
      if effect.extra_data.strategy == "snatch_courage" or effect.card:getMark("strategycourage") > 0 then
        local card = Fk:getCardById(cid)
        if card.trueName == "slash" or card.name == "lvshi_poison" then
          effect.from:turnOver()
          room:useVirtualCard(card.name, nil, effect.from, effect.to, self.name, true)
        end
      end
    end
  end,
})

return skill